// 监听页面的ajax
window.addEventListener("ajaxReadyStateChange", (e) => {
    const config = {
        rule: {
            method: "GET",
            indexStr: "/aweme/v1/web/aweme/favorite",
        }
    }

    let xhr = e.detail;
    if (xhr.readyState == 4 && xhr.status == 200) {
        // xhr.getAllResponseHeaders()  响应头信息
        // xhr.requestHeader            请求头信息
        // xhr.responseURL              请求的地址
        // xhr.responseText             响应内容
        // xhr.orignUrl                 请求的原始参数地址
        // xhr.body                     post参数，（get参数在url上面）

        if (xhr._byted_method === config.rule.method && xhr._byted_url.indexOf(config.rule.indexStr) !== -1) {
            const res = JSON.parse(xhr.response)
            const pre = res.aweme_list.map((aweme, aweme_index) => {
                // 处理数据结构
                const structure = {
                    title: aweme.preview_title,
                    desc: aweme.desc,
                    type: 0, // 0-没有 1-图文 2-视频
                    tag: aweme.video_tag.map((tag) => ({ name: tag.tag_name, level: tag.level })).filter((tag) => tag.name),
                    video: {},
                    images: [],
                    music: {
                        title: aweme.music.title,
                        cover: aweme.music.cover_large.url_list[0],
                        address: aweme.music.play_url.url_list[aweme.music.play_url.url_list.length - 1]
                    },
                }
                if (aweme.images) {
                    structure.images = aweme.img_bitrate[aweme.img_bitrate.length - 1].images.map((image) => ({
                        thumbnails: image.url_list[0],
                        complete: image.url_list[image.url_list.length - 1]
                    }));
                    structure.type = 1
                } else if (aweme.video.big_thumbs) {
                    structure.video = {
                        cover: aweme.video.cover.url_list[0],
                        address: aweme.video.play_addr.url_list[aweme.video.play_addr.url_list.length - 1],
                    }
                    structure.type = 2;
                }
                return structure;
            });
            console.log(pre)
            // 将数据传递到对应的服务，并保存
        }
    }
});